#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(ui->pushButton, &QPushButton::clicked, [ = ]() {
        posX += 20;
        update();//手动调用绘图事件
    });
    QTimer *timer = new QTimer(this);
    timer->start(1000);
    connect(timer, &QTimer::timeout, [ = ]() {
        posX += 20;
        update();//手动调用绘图事件
    });
}
Widget::~Widget()
{
    delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
    //实例化画家对象 this指的是画板
    // QPainter painter(this);
    // //设置画笔
    // QPen pen(QColor(255, 0, 0));
    //// QColor color = QColorDialog::getColor();
    //// pen.setColor(color);
    // //设置画笔宽度
    // pen.setWidth(3);
    // //设置画笔风格
    // pen.setStyle(Qt::DashDotDotLine);
    // //画家关联画笔
    // painter.setPen(pen);
    // //设置画刷
    // QBrush brush(Qt::cyan);
    // brush.setStyle(Qt::Dense5Pattern);
    // painter.setBrush(brush);
    // //画线
    // painter.drawLine(QPoint(0, 0), QPoint(100, 100));
    // //画圆或者椭圆
    // painter.drawEllipse(QPoint(100, 100), 50, 50);
    // //画矩形
    // painter.drawRect(QRect(20, 20, 50, 50));
    // //画文字
    // painter.drawText(QRect(10, 200, 150, 50), "正在书写文字");
///////////////////////////////////////////////////////////////////////
    // QPainter painter(this);
    //// painter.drawEllipse(QPoint(100, 50), 50, 50);
    //// //设置抗锯齿能力
    //// painter.setRenderHint(QPainter::Antialiasing);
    //// painter.drawEllipse(QPoint(200, 50), 50, 50);
    // painter.drawRect(QRect(20, 20, 50, 50));
    // //移动画家，即改变左上角坐标
    // painter.translate(100, 0);
    // //保存画家状态
    // painter.save();
    // painter.drawRect(QRect(20, 20, 50, 50));
    // //再次移动
    // painter.translate(100, 0);
    // //还原画家保存状态，即上述移动画家失效
    // painter.restore();
    // painter.drawRect(QRect(20, 20, 50, 50));
/////////////////画图片//////////
    QPainter painter(this);
    if(posX > this->width()) {
        posX = 0;
    }
    painter.drawPixmap(posX, 0, QPixmap(":/Image/1.png"));
}
